package com.htrq.entity;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 权限资源信息
 * @ClassName: TResource 
 * @Description: TODO
 * @author: Administrator
 * @date: 2017-5-10 下午03:35:38
 */
public class TResource implements Serializable {
	/**
	 * @fieldName: serialVersionUID
	 * @fieldType: long
	 * @Description: TODO
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	private String text;
	private int parentId;
	private boolean leaf = true;// 是否是子节点
	private String hrefTarget;// 页面url
	private int ifc;// 是否是操作类请求
	private String remark;
	private List<TResource> children = new ArrayList<TResource>();

	public TResource() {
	}
	public TResource(int id, String text, int parentId, String hrefTarget,int ifc, String remark) {
		this.id = id;
		this.text = text;
		this.parentId = parentId;
		this.hrefTarget = hrefTarget;
		this.ifc=ifc;
		this.remark = remark;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	public boolean isLeaf() {
		return leaf;
	}
	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}
	public String getHrefTarget() {
		return hrefTarget;
	}
	public void setHrefTarget(String hrefTarget) {
		this.hrefTarget = hrefTarget;
	}
	public int getIfc() {
		return ifc;
	}
	public void setIfc(int ifc) {
		this.ifc = ifc;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public List<TResource> getChildren() {
		return children;
	}
	public void setChildren(List<TResource> children) {
		this.children = children;
	}
	// 构建ext需要的树形结构的方法
	public static final <T> List<T> buildTree(List<T> nodes) {
		if (null == nodes || nodes.size() == 0)
			return null;
		Map<Integer, T> resources = new HashMap<Integer, T>();
		List<T> result = new ArrayList<T>();
		try {
			for (int i = 0; i < nodes.size(); i++) {
				T node = nodes.get(i);
				Method getId = node.getClass().getMethod("getId");
				Integer id = (Integer) getId.invoke(node);
				resources.put(id, node);
			}
			for (Map.Entry<Integer, T> e : resources.entrySet()) {
				T node = e.getValue();
				Method getparentId = node.getClass().getMethod("getParentId");
				Integer parentId = (Integer) getparentId.invoke(node);
				if (parentId == 0) {
					result.add(node);
				} else {
					T parent = resources.get(parentId);
					if (null != parent) {
						@SuppressWarnings("unchecked")
						List<T> children = (List<T>) parent.getClass().getMethod("getChildren").invoke(parent);
						children.add(node);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
